import util from '../../common/js/util'
import NProgress from 'nprogress'
import axios from 'axios';
import flow from "../../../models/dict/flow.js"  //任务字典模块
import address from "../../../models/dict/address.js"  //任务字典模块
import moment from 'moment'

import task from "../../../models/dict/task.js"  //任务字典模块
import array from "../../../models/comm/array.js"  //数组操作模块
import flowNode from './flowNode.vue'
import flowNodeList from './flowNodeList.vue'
export default {
    data() {
        return {
            filters: {
                name: ''
            },
            flowList: [],
            total: 0,
            page: 1,
            listLoading: false,
            sels: [],//列表选中列

            editFormVisible: false,//编辑界面是否显示
            editLoading: false,
            editFormRules: {
                name: [
                    { required: true, message: '请输入姓名', trigger: 'blur' }
                ]
            },
            //编辑界面数据1
            editForm: {
                id: 0,
                name: '',
                sex: -1,
                age: 0,
                birth: '',
                addr: ''
            },

            addFormVisible: false,//新增界面是否显示dd
            addLoading: false,
            ShowSave: false,
            addFormRules: {
                name: [
                    { required: true, message: '请输入姓名', trigger: 'blur' }
                ]
            },
            //新增界面数据
            addForm: {
                name: '',
                sex: -1,
                age: 0,
                birth: '',
                addr: ''
            },
            currObj: {


                address: {}
            },
            addressOptions: address.address,
            selectedOptions: ["330000", "330100", "330110"],


            v: [],
            classArray: flow.classArray(),
            ShowSaveNode: false,
            currNodeObj: {},

            taskClassArray: task.classArray(),
            isDefault: false


        }
    },
    methods: {
        //性别显示转换
        formatSex: function (row, column) {
            return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
        },
        fmtDate: function (time) {

            return moment(time).format("YYYY-MM-DD HH:mm:ss");

        },
        fmtaddress: (row, col) => {
            if (row.address) {

                return row.address.provinceName + " " + row.address.cityName
            }
            else {

                return "未知"
            }

        },
        addressChange: (value) => {

            var iii = value;

        },

        handleCurrentChange(val) {
            this.page = val;
            this.getFlowList();
        },
        //获取用户列表
        getFlowList() {

            this.listLoading = true;
            NProgress.start();
            axios.post("/a", {
                class: "flow",
                para: "getFlowList",
                j: {
                    data: {



                  //      commpanyCode: localStorage.commpanyCode


                    },
                    page: this.page


                }

            }).then((res) => {

                this.flowList = res.data.list;

                this.total = res.data.total;
                this.listLoading = false;
                NProgress.done();


            });
        },
        addFlow() {

            this.toSaveFlow();
        },

        //删除
        handleDel: function (index, row) {

        },



        //显示编辑界面
        toSaveFlow: function (index, row) {

            if (row) {

                axios.post("/a", {
                    class: "flow",
                    para: "getFlow",
                    j: {
                        _id: row._id
                    }

                }).then((res) => {

                    if (!res.data.info.class) {
                        res.data.info.class = [];


                    }


                    this.currObj = res.data.info;
                    if (this.currObj.isDefault) {
                        this.isDefault = true
                    }
                    else {
                        this.isDefault = false
                    }





                    this.selectedOptions = [this.currObj.address.provinceId, this.currObj.address.cityId, this.currObj.address.areaId]


                    this.joinData();

                });
            }
            else {
                //新增
                this.currObj = {

                    address: {},
                    class: "proof",
                    nodeList: [],
                    isDefault: false
                }

                this.currObj.nodeList = [{
                    id: "root",
                    name: "起始节点",
                    taskClassId: "root"
                }]
                this.joinData();
            }

            this.ShowSave = true;

        },
        changeDefault() {

        },
        //保存流程
        saveFlow: function () {

            this.currObj.address = address.addressObj(this.selectedOptions, this.currObj.address.memo);
            this.currObj.commpanyCode = localStorage.commpanyCode;
            this.currObj.isDefault = this.isDefault
            axios.post("/a", {
                class: "flow",
                para: "saveFlow",
                j: this.currObj

            }).then((res) => {
                if (res.data.re == "ok") {
                    this.ShowSave = false;
                    this.getFlowList();

                    this.$message({
                        message: '工作流程已经保存',
                        type: 'success'
                    });

                } else {
                    alert(JSON.stringify(res.data));
                }
            });


        },



        //准备新增节点
        toSaveNode: function (nodeObj) {




            if (nodeObj) {


                // if (nodeObj.id == "root") {
                //     return;
                // }

                this.currNodeObj = nodeObj;
            }

            if (!this.currNodeObj.taskClassId) {

                this.currNodeObj.taskClassId = 'default';
            }

            this.ShowSaveNode = true;
        },
        //保存节点
        saveNode: function () {


            if (!this.currObj.nodeList) {
                this.currObj.nodeList = [];

            }
            if (!this.currNodeObj.id) {
                this.currNodeObj.id = moment().utcOffset(-8).format("YYMMDDHHmmssSSS");
            }
            if (!this.currNodeObj.rely) {

                this.currNodeObj.rely = [];
            }

            var isAdd = true;
            this.currObj.nodeList.forEach(function (node) {

                if (this.currNodeObj.id == node.id) {
                    //当前节点的ID已经存在了. 就不是新增了
                    isAdd = false;
                }

            }, this);
            if (isAdd) {
                //如果是新增, 就增加一条
                this.currObj.nodeList.push(this.currNodeObj);
            }


            this.ShowSaveNode = false;
            this.$message({
                message: '节点已变更, 请不要忘记保存流程',
                type: 'success'
            });
            this.joinData();
        },
        joinData: function () {


            this.currObj.nodeList.forEach(function (node) {



                this.taskClassArray.forEach(function (taskClass) {
                    if (taskClass.id == node.taskClassId) {
                        // console.log("相等");
                        node.taskClassName = taskClass.name
                    }



                }, this);


            }, this);
        },

        exeCustomerOrderFlow() {

        },
        getClassName(classId) {
            return flow.getClassName(classId)

        }







    },


    mounted() {
        this.getFlowList();
    },

    components: {

        flowNode,
        flowNodeList

    }

}